﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using WorkSpace.Core;
using SmartSpace.Core;
using SmartSpace;

namespace WorkSpace.Services
{
    /// <summary>
    /// Summary description for WorkSpaceWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WorkSpaceWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public ListResult<Category> GetCategories()
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<Category> list = service.FindCategories(User.Identity.Name);
            return new ListResult<Category>(list);
        }

        [WebMethod]
        public ServiceResult SaveCategory(Category category)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            if (service.SaveCategory(category))
            {
                return new ServiceResult();
            }
            else
            {
                return new ServiceResult(false, "保存失败!");
            }
        }

        [WebMethod]
        public ListResult<Link> GetAllLinks(Guid categoryId, string account)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<Link> list = service.FindAllLinks(categoryId, account);
            return new ListResult<Link>(list);
        }

        [WebMethod]
        public ListResult<Link> GetInheritiedLinks()
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<Link> list = service.FindInheritedLinks(User.Identity.Name);
            return new ListResult<Link>(list);
        }

        [WebMethod]
        public ListResult<Link> GetPrivateLinks(Guid categoryId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<Link> list = service.FindPrivateLinks(categoryId, User.Identity.Name);
            return new ListResult<Link>(list);
        }

        [WebMethod]
        public ServiceResult SetEnabled(Guid linkId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            Link link = service.FindLink(linkId);
            if (link != null)
            {
                link.IsEnable = true;
                if (service.SaveLink(link))
                {
                    return new ServiceResult();
                }
            }
            return new ServiceResult(false, "保存失败!");
        }

        [WebMethod]
        public ServiceResult SetDisabled(Guid linkId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            Link link = service.FindLink(linkId);
            if (link != null)
            {
                link.IsEnable = false;
                if (service.SaveLink(link))
                {
                    return new ServiceResult();
                }
            }
            return new ServiceResult(false, "保存失败!");
        }

        [WebMethod]
        public ServiceResult SaveLink(Link link)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            if (service.SaveLink(link))
            {
                return new ServiceResult();
            }
            else
            {
                return new ServiceResult(false, "保存失败!");
            }
        }

        #region personal settings

        [WebMethod]
        public ListResult<Category> GetPersonalCategories(string account)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<Category> list = service.FindPersonalCategories(User.Identity.Name);
            return new ListResult<Category>(list);
        }

        [WebMethod]
        public ServiceResult SavePersonalPackage(PersonalPackage package)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            if (service.SavePersonalPackage(package))
            {
                return new ServiceResult();
            }
            else
            {
                return new ServiceResult(false, "保存失败!");
            }
        }

        [WebMethod]
        public ServiceResult RemovePersonalPackage(Guid packageId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            if (service.RemovePersonalPackage(packageId))
            {
                return new ServiceResult();
            }
            else
            {
                return new ServiceResult(false, "删除失败!");
            }
        }

        [WebMethod]
        public ListResult<PersonalPackage> GetPersonalPackages(Guid categoryId, string account)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<PersonalPackage> packages = service.FindPersonalPackages(categoryId, account);
            if (packages == null || packages.Count == 0)
            {
                // 有连接可用
                List<Link> links = service.FindAllLinks(categoryId, User.Identity.Name);
                if (links.Count > 0)
                {
                    // 创建默认的Package
                    PersonalPackage defaultPackage = new PersonalPackage();
                    defaultPackage.Id = Guid.NewGuid();
                    defaultPackage.Caption = "默认页";
                    defaultPackage.Account = User.Identity.Name;
                    defaultPackage.CategoryId = categoryId;
                    if (service.SavePersonalPackage(defaultPackage))
                    {
                        foreach (var link in links)
                        {
                            // 分配到默认页上
                            service.AddPersonalLink(defaultPackage, link.Id);
                        }
                    }
                    packages = service.FindPersonalPackages(categoryId, User.Identity.Name);
                }
            }
            return new ListResult<PersonalPackage>(packages);
        }

        [WebMethod]
        public ServiceResult AddPersonalLink(Guid packageId, Guid linkId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            PersonalPackage package = service.FindPersonalPackage(packageId);
            if (package != null)
            {
                if (service.AddPersonalLink(package, linkId))
                {
                    return new ServiceResult();
                }
            }
            return new ServiceResult(false, "保存失败!");
        }

        [WebMethod]
        public ServiceResult RemovePersonalLink(Guid packageId, Guid linkId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            PersonalPackage package = service.FindPersonalPackage(packageId);
            if (package != null)
            {
                if (service.RemovePersonalLink(package, linkId))
                {
                    return new ServiceResult();
                }
            }
            return new ServiceResult(false, "保存失败!");
        }

        [WebMethod]
        public ServiceResult AddPersonalLinks(Guid packageId, List<Guid> linkIds)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            PersonalPackage package = service.FindPersonalPackage(packageId);
            if (package != null)
            {
                service.ClearPersonalLinks(package);

                foreach (Guid linkId in linkIds)
                {
                    service.AddPersonalLink(package, linkId);
                }
            }
            return new ServiceResult(true, "保存完毕!");
        }

        [WebMethod]
        public ListResult<PersonalLink> GetPersonalLinks(Guid packageId)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<PersonalLink> list = service.FindPersonalLinks(packageId, User.Identity.Name);
            return new ListResult<PersonalLink>(list);
        }

        //[WebMethod]
        //public ListResult<Link> GetPersonalAllLinks(Guid categoryId, string account)
        //{
        //    IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
        //    List<Link> list = service.FindAllLinks(categoryId, account);
        //    return new ListResult<Link>(list);
        //}
        #endregion

        #region icon

        [WebMethod]
        public ServiceResult SaveIcon(IconData icon)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            if (service.SaveIcon(icon))
            {
                return new ServiceResult();
            }
            else
            {
                return new ServiceResult(false, "保存失败!");
            }
        }

        [WebMethod]
        public ListResult<IconData> FindIcons()
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            List<IconData> list = service.FindIcons();
            return new ListResult<IconData>(list);
        }

        [WebMethod]
        public SingleResult<IconData> FindIcon(Guid id)
        {
            IWorkSpaceService service = WorkSpaceWebContainer.Get<IWorkSpaceService>();
            IconData icon = service.FindIcon(id);
            return new SingleResult<IconData>(true,"message", icon);
        }

        #endregion

        [WebMethod]
        public SingleResult<int> GetTaskCount(Guid linkId, string account)
        {
            return new SingleResult<int>(true, "获取成功!", new Random().Next(0, 99));
        }
    }
}
